package com.zl.freight.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.zl.freight.R;
import com.zl.freight.mode.BaseUserEntity;
import com.zl.freight.service.IsLoginService;
import com.zl.freight.ui.activity.LoginActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhanglei on 2017\12\8 0008.
 * 系统功能工具类
 */

public class SystemUtils {

    private static final String TAG = "SystemUtils";

    /**
     * 调用拨号界面
     *
     * @param phone 电话号码
     */
    public static void call(Context context, String phone) {

        BaseUserEntity userData = SpUtils.getUserData(context);
        if (!userData.getIsCheck().equals("1")) {
            new AlertDialog.Builder(context)
                    .setMessage("您的实名认证还未通过无法拨打电话")
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    }).show();
            return;
        }

        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(context, "无法拨打电话，手机号未被获取", Toast.LENGTH_SHORT).show();
            return;
        }

        /**
         * 插入拨打电话的记录
         */
        Map<String, String> params = new HashMap<>();
        params.put("UserId", userData.getId());
        params.put("UserName", userData.getRealName());
        params.put("PayName", "有意拨打电话");
        params.put("Moreorless", "2");
        params.put("Value", phone);

        SoapUtils.Post(API.InsertIntegralChange, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                Log.e(TAG, error);
            }

            @Override
            public void onSuccess(String data) {
                Log.e(TAG, data);
            }
        });

        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 调用发短信界面
     *
     * @param context
     * @param phone
     */
    public static void sendSms(Context context, String phone) {
        Uri uri2 = Uri.parse("smsto:" + phone);
        Intent intentFinalMessage = new Intent(Intent.ACTION_VIEW, uri2);
//        intentFinalMessage.setType("vnd.android-dir/mms-sms");
        context.startActivity(intentFinalMessage);
    }

    public static void isLogin(final Activity mActivity) {

        //是否启动检测是否被挤掉
        if (!SpUtils.isLogin(mActivity)) {
            return;
        }

        //检测是否被别人挤掉了
        Map<String, String> map = new HashMap<>();
        map.put("UserId", SpUtils.getUserData(mActivity).getId());
        map.put("RegistrationId", SpUtils.getRegId(mActivity));
        SoapUtils.Post(API.CheckUser, map, new SoapCallback() {
            @Override
            public void onError(String error) {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(mActivity, R.string.is_login_hint, Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onSuccess(String data) {
                final Boolean aBoolean = Boolean.valueOf(data);
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!aBoolean) {//被人挤掉，提示用户
                            int role = SpUtils.getRole(mActivity);
                            Intent intent = new Intent(mActivity, LoginActivity.class);
                            intent.putExtra("role", role);
                            intent.putExtra(API.ISFINISH, true);
                            mActivity.startActivity(intent);
                            Toast.makeText(mActivity, R.string.is_login_hint, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

}
